package Dao;

import DataClass.BookBorrowingRequestRecord;
import Util.JBDCUtil;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

/**
 * @since 2024-06-01
 * @author yooo_fan
 * 数据库访问对象
 */
public class BookBorrowingRecordDao {
    private static PreparedStatement getRecordStatement = null;

    private static PreparedStatement insertRecordStatement = null;

    public static List<BookBorrowingRequestRecord> getBookBorrowingRequestRecord() throws SQLException {
        List<BookBorrowingRequestRecord> list = new ArrayList<>();
        ResultSet data;
        try{
            data = BookBorrowingRecordDao.getBookBorrowingData();
        }catch (SQLException e){
            System.out.println("图书借阅查询失败 from Dao");
            e.printStackTrace();
            throw new SQLException();
        }

        while(data.next()){
            Integer rId = data.getInt("Rid");
            String rBookId = data.getString("RBookid");
            String rRecord = data.getString("RRecord");
            Timestamp rTime = data.getTimestamp("RTime");
            String rPeople = data.getString("RPeople");
            String rbName = data.getString("RBName");
            list.add(new BookBorrowingRequestRecord(rId,rBookId,rRecord,rTime,rPeople,rbName));
        }

        // 释放资源
        JBDCUtil.closeResultSet(data);

        return list;
    }

    public static Boolean addBookBorrowingRequestRecord(BookBorrowingRequestRecord bookBorrowingRequestRecord) throws SQLException {
        try{
            System.out.println("in Dao addBookBorrowingRequestRecord");
            return BookBorrowingRecordDao.insertBookBorrowingRecord(bookBorrowingRequestRecord) != 0;
        }catch (SQLException e){
            System.err.println("插入数据借阅信息失败");
            throw new SQLException();
        }
    }

    /**
     * 获取图书借阅信息
     */
    public static ResultSet getBookBorrowingData() throws SQLException {
        if(getRecordStatement == null || getRecordStatement.isClosed())
        {
            String sql = "select * from Book_Borrowing_Request_Record";
            // 设置statement对象，解析SQL语句，并且保存，节省重复语句解析开销
            getRecordStatement = JBDCUtil.getPreparedStatement(sql);
        }

        ResultSet result = getRecordStatement.executeQuery();

        JBDCUtil.commit();
        return result;
    }

    /**
     * 插入一条图书借阅信息
     * @param record 图书记录对象
     * @return int 插入数据数量
     * @throws SQLException 数据库异常
     */
    public static int insertBookBorrowingRecord(BookBorrowingRequestRecord record) throws SQLException {

        if(insertRecordStatement == null)
        {
            String sql = "INSERT INTO Book_Borrowing_Request_Record (rBookId, rRecord, rTime, rPeople, rbName) VALUES (?, ?, ?, ?, ?)";
            insertRecordStatement = JBDCUtil.getPreparedStatement(sql);
        }

        insertRecordStatement.setString(1, record.getRBookId());
        insertRecordStatement.setString(2, record.getRRecord());
        // 将字符串日期时间转换为Timestamp
        insertRecordStatement.setTimestamp(3, record.getRTime());
        insertRecordStatement.setString(4, record.getRPeople());
        insertRecordStatement.setString(5, record.getRbName());

        int result = insertRecordStatement.executeUpdate();
        JBDCUtil.commit();
        return  result;
    }
}
